import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {PERMISSION} from './constants';
import {FORM_OPTIONS} from '../constants';
import WrappedSelect from '../common/WrappedSelect';
import {hasPermissions} from './utils';
import {dealerType} from '../Enum';
const ButtonGroup = Button.Group;
const FormItem = Form.Item;
import routes from './routes';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
const dealerConditions = {
    type: dealerType.总代
};
/*eslint-disable  react/prefer-stateless-function*/
class QueryPanel extends React.PureComponent {
    state = {
        loading: false
    }
    handleFilter = (value, name) => {
        this.props.onConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.onClickQuery();
    };

    onClickQuery = () => {
        this.props.querySearch();
    }

    render() {
        const operations = [];
        const otherBtns = [];
        if(hasPermissions(this.props.permissions, PERMISSION.apply))
            otherBtns.push(
                <Button key={PERMISSION.apply} href={`#${routes.apply.url()}`}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.apply',
                        defaultMessage: '申请'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.reject))
            otherBtns.push(
                <Button key={PERMISSION.reject} href={`#${routes.reject.url()}`}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.reject',
                        defaultMessage: '驳回'
                    })}
                </Button>
            );

        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.conditions.dealerId,
            code: this.props.conditions.dealerCode,
            name: this.props.conditions.dealerName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={dealerConditions}
                                    value={dealerInfo}
                                    onSelect={this.props.onSelect} />
                            </FormItem>
                        </Col>
                        {this.props.initEnum.brands.length > 1 ? <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="brandId"
                                    allowClear
                                    value={this.props.conditions.brandId}
                                    options={this.props.initEnum.brands}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col> : null}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '箱单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.pickingListCode',
                                    defaultMessage: '备料单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="pickingListCode"
                                    value={this.props.conditions.pickingListCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.partSaleOrderCode',
                                    defaultMessage: '订单编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="partSaleOrderCode"
                                    value={this.props.conditions.partSaleOrderCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.partOrderTypeId',
                                    defaultMessage: '订单类型'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="partOrderTypeId"
                                    value={this.props.conditions.partOrderTypeId}
                                    options={this.props.initEnum.partSaleOrderTypes}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.onClickQuery}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            <ButtonGroup>{otherBtns}</ButtonGroup>
                            {operations}
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnum: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, selectDealer} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);


const getInitData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        const partSaleOrderTypes = initData.partSaleOrderTypes ? initData.partSaleOrderTypes.map(item => ({
            text: `${item.typeName}`,
            value: item.id
        })) : [];
        return {
            brands,
            partSaleOrderTypes
        };
    }
);

const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    initEnum: getInitData(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealer(data))
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
